﻿using CommonBehaviors.Actions;
using YourRaidingBuddy.Core;
using YourRaidingBuddy.Core.Helpers;
using YourRaidingBuddy.Core.Managers;
using Styx;
using Styx.TreeSharp;
using Styx.WoWInternals.WoWObjects;
using System.Windows.Forms;
using G = YourRaidingBuddy.Rotations.Global;
using I = YourRaidingBuddy.Core.Item;
using Lua = YourRaidingBuddy.Core.Helpers.LuaClass;
using T = YourRaidingBuddy.Core.Managers.TalentManager;
using SG = YourRaidingBuddy.Interfaces.Settings.InternalSettings;
using SH = YourRaidingBuddy.Interfaces.Settings.SettingsH;
using U = YourRaidingBuddy.Core.Unit;
using Styx.CommonBot;
using Styx.WoWInternals;

namespace YourRaidingBuddy.Rotations.Rogue
{
    class Assassination
    {
        private static LocalPlayer Me { get { return StyxWoW.Me; } }

        #region Initialize Rotations
        internal static Composite InitializeAss
        {
            get
            {
                return new PrioritySelector(
                    new Decorator(ret => (HotKeyManager.IsPaused || !U.DefaultCheck), new ActionAlwaysSucceed()),           
                        G.ManualCastPause(),
                       G.InitializeOnKeyActionsR(),
                    //   new Action(delegate { YBLogger.AdvancedLogP("PoisonNo: = {0}", Poisons.CreateApplyPoisons()); return RunStatus.Failure; }),
                   //    new Decorator(ret => SG.Instance.General.CheckAWaancedLogging, WaLogger.AWaancedLogging),
                       new Decorator(ret => HotKeyManager.IsSpecial, new PrioritySelector(Spell.Cast("Feint", ret => SG.Instance.Assassination.EnableFeintUsage && !Me.HasAura("Feint")))),
                        new Decorator(ret => !Spell.IsGlobalCooldown() && SH.Instance.ModeSelection == Enum.Mode.Auto,
                            new PrioritySelector(
                                new Decorator(ret => SG.Instance.Assassination.CheckAutoAttack,
                                    Lua.StartAutoAttack),
                                //new Decorator(ret => Me.HealthPercent < 100,
                                //    AssaDefensive()),
                                new Decorator(ret => SG.Instance.Assassination.CheckInterrupts,
                                    AssaInterrupts()),
                                AssaUtility(),
                          new Action(ret => { Item.UseAssassinationItems(); return RunStatus.Failure; }),
                          new Decorator(ret => SG.Instance.General.CheckPotionUsage && G.SpeedBuffsAura, Item.UseBagItem(76089, ret => true, "Using Virmen's Bite Potion")),
                                AssaOffensive(),
                                new Decorator(ret => SG.Instance.Assassination.CheckAoE && U.NearbyAttackableUnitsCount >= 2, AssaMt()),
                                    AssaSt())),
                        new Decorator(ret => !Spell.IsGlobalCooldown() && SH.Instance.ModeSelection == Enum.Mode.SemiHotkey,
                            new PrioritySelector(
                                new Decorator(ret => SG.Instance.Assassination.CheckAutoAttack,
                                    Lua.StartAutoAttack),
                                //new Decorator(ret => Me.HealthPercent < 100,
                                //    AssaDefensive()),
                                new Decorator(ret => SG.Instance.Assassination.CheckInterrupts,
                                    AssaInterrupts()),
                                AssaUtility(),
                           new Decorator(ret => HotKeyManager.IsCooldown,
                                    new PrioritySelector(
                               new Action(ret => { Item.UseAssassinationItems(); return RunStatus.Failure; }),
                               new Decorator(ret => SG.Instance.General.CheckPotionUsage && G.SpeedBuffsAura, Item.UseBagItem(76089, ret => true, "Using Virmen's Bite Potion")),
                                        AssaOffensive())),
                                new Decorator(ret => SG.Instance.Assassination.CheckAoE && U.NearbyAttackableUnitsCount >= 2, AssaMt()),
                                    AssaSt())),
                        new Decorator(ret => !Spell.IsGlobalCooldown() && SH.Instance.ModeSelection == Enum.Mode.Hotkey,
                            new PrioritySelector(
                                new Decorator(ret => SG.Instance.Assassination.CheckAutoAttack,
                                    Lua.StartAutoAttack),
                                //new Decorator(ret => Me.HealthPercent < 100,
                                //    AssaDefensive()),
                                new Decorator(ret => SG.Instance.Assassination.CheckInterrupts,
                                    AssaInterrupts()),
                                AssaUtility(),
                                new Decorator(ret => HotKeyManager.IsCooldown,
                                    new PrioritySelector(
                               new Action(ret => { Item.UseAssassinationItems(); return RunStatus.Failure; }),
                               new Decorator(ret => SG.Instance.General.CheckPotionUsage && G.SpeedBuffsAura, Item.UseBagItem(76089, ret => true, "Using Virmen's Bite Potion")),
                                        AssaOffensive())),
                                new Decorator(ret => HotKeyManager.IsAoe, AssaMt()),
                                new Decorator(ret => !HotKeyManager.IsAoe, AssaSt()))));
            }
        }
        #endregion

        #region Rotations
        internal static Composite AssaSt()
        {
            return new PrioritySelector(

                Spell.Cast("Dispatch", ret => Me.ComboPoints < 5 && G.DispatchProc),
                //Initial pull / fresh target no rupture on
                new Decorator(ret => (G.TargetNoRupture && Me.CurrentTarget.HealthPercent >= 35.0),
                    new PrioritySelector(
                        Spell.Cast("Mutilate", ret => Me.ComboPoints < 5),
                        //Spell.Cast("Dispatch", ret => Me.ComboPoints < 5 && G.DispatchProc),
                        Spell.Cast("Rupture", ret => Me.ComboPoints > 4 && (G.TargetNoRupture && Me.CurrentTarget.MaxHealth > SG.Instance.General.MinHPAbility)), //Full rupture
                        Spell.Cast("Rupture", ret => Me.ComboPoints > 2 && (G.TargetNoRupture && Me.CurrentTarget.MaxHealth < SG.Instance.General.MinHPAbility)) //less 
                    )),
                
                //Rupture on target, CP Builders
                new Decorator(ret => (G.TargetHaveRupture && Me.CurrentTarget.HealthPercent >= 35.0),
                    new PrioritySelector(
                        Spell.Cast("Mutilate", ret =>(G._anticipationCount < 4 || Me.ComboPoints < 5)),
                        Spell.Cast("Rupture", ret => (Me.ComboPoints > 1 && G.TargetRuptureFalling) || (Me.ComboPoints > 4 && G.TargetRuptureFalling5Cps)),
                        Spell.Cast("Envenom", ret => Me.ComboPoints > 4 && (!Me.HasAura(32645) || G.EnvenomRemains2Seconds))
                        //Spell.Cast("Dispatch", ret => (G._anticipationCount < 4 || Me.ComboPoints < 5) || (G.DispatchProc))

                )),

                //Dispatch Range

                 new Decorator(ret => (G.TargetNoRupture && Me.CurrentTarget.HealthPercent < 35.0),
                    new PrioritySelector(
                        Spell.Cast("Dispatch", ret => Me.ComboPoints < 5 ),
                        Spell.Cast("Rupture", ret => Me.ComboPoints > 4 && (G.TargetNoRupture && Me.CurrentTarget.MaxHealth > SG.Instance.General.MinHPAbility)), //Full rupture
                        Spell.Cast("Rupture", ret => Me.ComboPoints > 2 && (G.TargetNoRupture && Me.CurrentTarget.MaxHealth < SG.Instance.General.MinHPAbility)) //less 
                    )),


                new Decorator(ret => (G.TargetHaveRupture && Me.CurrentTarget.HealthPercent < 35.0),
                    new PrioritySelector(
                        Spell.Cast("Rupture", ret => (Me.ComboPoints > 1 && G.TargetRuptureFalling) || (Me.ComboPoints > 4 && G.TargetRuptureFalling5Cps)),
                        Spell.Cast("Envenom", ret => Me.ComboPoints > 4 && (!Me.HasAura(32645) || G.EnvenomRemains2Seconds)),
                        Spell.Cast("Dispatch", ret => (G._anticipationCount < 4 || Me.ComboPoints < 5) || (G.DispatchProc))

                ))
                
                );
          
        }

        static Composite AssaMt()
        {
            return new PrioritySelector(
              new Decorator(ret => HotKeyManager.IsSpecial, new PrioritySelector(Spell.Cast("Feint", ret => SG.Instance.Assassination.EnableFeintUsage && !Me.HasAura("Feint")))),
              Spell.Cast("Fan of Knives", ret => Me.ComboPoints < 5),
              Spell.Cast("Rupture", ret => Me.ComboPoints > 1 && G.TargetRuptureFalling && U.NearbyAttackableUnitsCount <= 2),
              Spell.Cast("Envenom", ret => Me.ComboPoints > 4)
              //Spell.Cast("Slice and Dice", ret => Me.ComboPoints > 1 && G.FucknoSND)
                );
        }

        //static Composite AssaDefensive()
        //{
        //    return new PrioritySelector(
        //        Spell.Cast("Shadowstep", ret => SG.Instance.Assassination.CheckShadowstep && Me.HealthPercent < SG.Instance.Assassination.NumShadowstep),
        //        Spell.Cast("Recuperate", ret => SG.Instance.Assassination.CheckRecuperate && Me.HealthPercent < SG.Instance.Assassination.NumRecuperate && Me.ComboPoints >= SG.Instance.Assassination.NumRecuperateCombo),
        //        I.AssassinationUseHealthStone()
        //        );
        //}

        static Composite AssaOffensive()
        {
            return new PrioritySelector(
                //Spell.Cast("Shadow Blades", ret => (G.SpeedBuffsAura || G.IloveyouSND) && (
                //    (SG.Instance.Assassination.ShadowBlades == Enum.AbilityTrigger.OnBossDummy && U.IsTargetBoss) ||
                //    (SG.Instance.Assassination.ShadowBlades == Enum.AbilityTrigger.OnBlTwHr && (G.SpeedBuffsAura)) ||
                //    (SG.Instance.Assassination.ShadowBlades == Enum.AbilityTrigger.Always)
                //    )),
                Spell.Cast("Berserking", ret => Me.Race == WoWRace.Troll && (G.IloveyouSND || G.SpeedBuffsAura && G.TargetIsClose) && (
                    (SG.Instance.Assassination.ClassRacials == Enum.AbilityTrigger.OnBossDummy && U.IsTargetBoss) ||
                    (SG.Instance.Assassination.ClassRacials == Enum.AbilityTrigger.OnBlTwHr && (G.SpeedBuffsAura)) ||
                    (SG.Instance.Assassination.ClassRacials == Enum.AbilityTrigger.Always)
                    )),
                Spell.Cast("Blood Fury", ret => Me.Race == WoWRace.Orc && (G.IloveyouSND || G.SpeedBuffsAura && G.TargetIsClose) && (
                    (SG.Instance.Assassination.ClassRacials == Enum.AbilityTrigger.OnBossDummy && U.IsTargetBoss) ||
                    (SG.Instance.Assassination.ClassRacials == Enum.AbilityTrigger.OnBlTwHr && (G.SpeedBuffsAura)) ||
                    (SG.Instance.Assassination.ClassRacials == Enum.AbilityTrigger.Always)
                    )),
                Spell.Cast("Rocket Barrage", ret => Me.Race == WoWRace.Goblin && (G.IloveyouSND || G.SpeedBuffsAura && G.TargetIsClose) && (
                    (SG.Instance.Assassination.ClassRacials == Enum.AbilityTrigger.OnBossDummy && U.IsTargetBoss) ||
                    (SG.Instance.Assassination.ClassRacials == Enum.AbilityTrigger.OnBlTwHr && (G.SpeedBuffsAura)) ||
                    (SG.Instance.Assassination.ClassRacials == Enum.AbilityTrigger.Always)
                    )),
                    Spell.Cast("Vendetta", ret => (G.IloveyouSND || G.SpeedBuffsAura) && G.TargetIsClose && (
                    (SG.Instance.Assassination.Vendetta == Enum.AbilityTrigger.OnBossDummy && U.IsTargetBoss) ||
                    (SG.Instance.Assassination.Vendetta == Enum.AbilityTrigger.OnBlTwHr && (G.SpeedBuffsAura)) ||
                    (SG.Instance.Assassination.Vendetta == Enum.AbilityTrigger.Always)
                    )),
                    Spell.Cast("Marked for Death", ret => G.IloveyouSND && (
                    (SG.Instance.Assassination.Tier6Abilities == Enum.AbilityTrigger.OnBossDummy && U.IsTargetBoss) ||
                    (SG.Instance.Assassination.Tier6Abilities == Enum.AbilityTrigger.OnBossDummy && (G.SpeedBuffsAura)) ||
                    (SG.Instance.Assassination.Tier6Abilities == Enum.AbilityTrigger.Always)
                    )));
        }

        internal static Composite AssaUtility()
        {
            return new PrioritySelector(

                );
        }

        internal static Composite AssaInterrupts()
        {
            return new PrioritySelector(
                   new Throttle(1, System.TimeSpan.FromMilliseconds(G._random.Next(SG.Instance.General.InterruptStart, SG.Instance.General.InterruptEnd)), RunStatus.Failure,
                    Spell.Cast("Kick", ret => (SG.Instance.General.InterruptList == Enum.InterruptList.MoP && (G.InterruptListMoP.Contains(Me.CurrentTarget.CurrentCastorChannelId()))) ||
                    (SG.Instance.General.InterruptList == Enum.InterruptList.NextExpensionPack && (G.InterruptListTBA.Contains(Me.CurrentTarget.CurrentCastorChannelId()))))),
                    new Throttle(1, System.TimeSpan.FromMilliseconds(G._random.Next(SG.Instance.General.InterruptStart, SG.Instance.General.InterruptEnd)), RunStatus.Failure,
                    Spell.Cast("Deadly Throw", ret => G.Kick && TalentManager.IsSelected(4) && Me.ComboPoints > 2 && (
                    (SG.Instance.General.InterruptList == Enum.InterruptList.MoP && (G.InterruptListMoP.Contains(Me.CurrentTarget.CurrentCastorChannelId()))) ||
                    (SG.Instance.General.InterruptList == Enum.InterruptList.NextExpensionPack && (G.InterruptListTBA.Contains(Me.CurrentTarget.CurrentCastorChannelId())))))
                    ));
        }  
        #endregion

        #region Tricks of the trade
        private static WoWUnit TricksTarget
        {
            get
            {
                return G.BestTricksTarget;
            }
        }
        #endregion
    }
}
